package com.yangkunhong.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yangkunhong.constant.ErrMsg;
import com.yangkunhong.pojo.User;
import com.yangkunhong.service.UserService;
import com.yangkunhong.utils.DateUtils;
import com.yangkunhong.utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;

@WebServlet(urlPatterns = {
        "/UserServlet136/reg","/UserServlet136/login",
        "/UserServlet136/center","/UserServlet136/password",
        "/UserServlet136/avatar","/UserServlet136/logout"
})
public class UserServlet136 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/reg")){
            req.getRequestDispatcher("/frontend/reg.jsp").forward(req,resp);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/center")){
            req.setAttribute("active","center");
            req.getRequestDispatcher("/frontend/user_center.jsp").forward(req,resp);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/password")){
            req.setAttribute("active","password");
            req.getRequestDispatcher("/frontend/user_center.jsp").forward(req,resp);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/avatar")){
            req.setAttribute("active","avatar");
            req.getRequestDispatcher("/frontend/user_center.jsp").forward(req,resp);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/logout")){
            //退出登录
           req.getSession().removeAttribute("USER_INFO");
           resp.sendRedirect("/IndexServlet001");
        }else{
            req.getRequestDispatcher("/frontend/login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // System.out.println(AjaxUtils.isAjaxRequest(req));
        //System.out.println(AjaxUtils.parseJsonToVo(req, User.class));

        //1 接收参数
        String json = JSONUtils.parseJsonInputStream(req);
        //2 解析数据
        Gson gson = new Gson();
        Type mapType = new TypeToken<Map<String, String>>() {}.getType();
        Map map = gson.fromJson(json, mapType);
        //3 绑定数据
        //获取动态IP
        map.put("ip",req.getRemoteAddr());
        map.put("userAgent",req.getHeader("User-Agent"));
        //区别当前请求是登陆还是注册
        //4 调用Service【业务逻辑】
        UserService userService = new UserService();
        ErrMsg errMsg = ErrMsg.CAPTCHA_ERR.OK;
        if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/reg")){
             errMsg = userService.RegisterUser(map);
        }else if(req.getRequestURI().equals(req.getContextPath()+"/UserServlet136/password")){
            //定义参数
            User user = new User();
            User userInfo = (User) req.getSession().getAttribute("USER_INFO");
            if(userInfo == null){
                JSONUtils.sendJsonStr( ErrMsg.NO_LOGIN,resp);
            }
            user.setId(userInfo.getId());
            user.setPassword(userInfo.getPassword());
            user.setLastAt(DateUtils.getLocalDateString());
            user.setLastIp(req.getRemoteAddr());
            //调用服务层
             errMsg = userService.doUpdatePassword(user, map, req);
        }else{
             errMsg = userService.doLogin(map,req);
        }
        //5 发送数据到浏览器【做出响应】
        JSONUtils.sendJsonStr(errMsg,resp);

    }
}
